﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TicketApp.Web.TGIServiceProxy;
using System.Configuration;
using System.Data;

namespace TicketApp.Web.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var customers = context.AllUsers.Where(user => user is Customer);
            return View(customers);
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();

            return View(customer);
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {

            if (ModelState.IsValid)
            {
                var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                
                //context.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly;
                var role = context.Roles.Where(r => r.Name.ToLower() == "customer").FirstOrDefault();
               // var address = new Address() { Line1 = "Line1", Line2 = "Line2", PostalCode = "1111", State = "State", City = "City", Country = "Country" };
                var address = customer.Address;
                context.AddToAddresses(address);
                

               // user.Address = address;
                customer.Role = role;
                
                context.AddToAllUsers(customer);
                
                context.SetLink (customer, "Role", role); 
                context.SetLink(customer, "Address", address); 

                context.SaveChanges();
                
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();
            return View(customer);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Customer customer)
        {

            if (ModelState.IsValid)
            {
                var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                context.AttachTo("AllUsers", customer);
                context.AttachTo("Addresses", customer.Address);
                context.UpdateObject(customer.Address);
                context.UpdateObject(customer);
                
                context.SaveChanges( System.Data.Services.Client.SaveChangesOptions.Batch);

            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();
            return View(customer);
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Customer customerToBeDeleted)
        {

            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AttachTo("AllUsers", customerToBeDeleted);
            context.AttachTo("Addresses", customerToBeDeleted.Address);
            context.DeleteObject(customerToBeDeleted);
            context.DeleteObject(customerToBeDeleted.Address);
            context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);
            return RedirectToAction("Index");

           
        }
    }
}
